package cx.ath.skyflyer.gpslog.nmea;

import j2meunit.framework.Test;
import j2meunit.framework.TestCase;
import j2meunit.framework.TestMethod;
import j2meunit.framework.TestSuite;

/**
 *
 * @author Ahti Legonkov
 */
public class ChecksumTest extends TestCase {

    public ChecksumTest() {

    }

    private ChecksumTest(String name, TestMethod method) {
        super(name, method);
    }

    public void setUp() throws Exception {
        super.setUp();
    }

    public void tearDown() throws Exception {
        super.tearDown();
    }

    private class Pair {
        public Pair(String s, byte b) {
            string = s;
            checksum = b;
        }

        public final String string;
        public final byte checksum;
    }

    private void testCalculate() {
        Pair[] data = {
            new Pair("$GPGGA,223027.826,5859.2124,N,02443.4099,E,0,0,,129.7,M,20.1,M,,*46", (byte)0x46),
            new Pair("GPGGA,223027.826,5859.2124,N,02443.4099,E,0,0,,129.7,M,20.1,M,,", (byte)0x46),
            //new Pair("$GPRMC,223027.826,V,5859.2124,N,02443.4099,E,0.00,0.00,240709,,,N*75", (byte)0x75),
            //new Pair("$GPGGA,223028.626,9000.0000,N,00000.0000,E,0,0,,-20.1,M,20.1,M,,*5C", (byte)0x5C),
            //new Pair("$GPRMC,223028.626,V,9000.0000,N,00000.0000,E,0.00,0.00,240709,,,N*7C", (byte)0x7C),
            //new Pair("$GPGGA,223029.626,8960.0000,N,00000.0000,E,0,0,,137.0,M,13.0,M,,*49", (byte)0x49),
            //new Pair("$GPRMC,223029.626,V,8960.0000,N,00000.0000,E,0.00,0.00,240709,,,N*73", (byte)0x73),
            new Pair("$GPGGA,223030.626,8960.0000,N,00000.0000,E,0,0,,137.0,M,13.0,M,,*41", (byte)0x41),
            new Pair("GPGGA,223030.626,8960.0000,N,00000.0000,E,0,0,,137.0,M,13.0,M,,", (byte)0x41),
        };

        for (int i = 0; i < data.length; ++i) {
            byte checksum = Checksum.calculate(data[i].string);
            byte expected = data[i].checksum;
            assertEquals(expected, checksum);
        }
    }

    private class StringBool {
        public StringBool(String s, boolean b) {
            string = s;
            result = b;
        }

        public final String string;
        public final boolean result;
    }

    private void testCheck() {
        try {
            StringBool[] data = {
                new StringBool("$GPGGA,223027.826,5859.2124,N,02443.4099,E,0,0,,129.7,M,20.1,M,,*46", true),
                new StringBool("$GPGGA,090405.000,5859.2173,N,02443.4196,E,1,8,1.06,52.8,M,20.1,M,,*6A", true),
                new StringBool("$GPRMC,223027.826,V,5859.2124,N,02443.4099,E,0.00,0.00,240709,,,N*7F", false),
            };

            for (int i = 0; i < data.length; ++i) {
                boolean result = Checksum.check(data[i].string);
                assertEquals(data[i].result, result);
            }

        }
        catch (Exception ex) {
            fail("Unexpected:" + ex);
        }
    }

    public Test suite() {
        TestSuite ts = new TestSuite();

		ts.addTest(new ChecksumTest("testCalculate", new TestMethod() {
            public void run(TestCase tc) {
                ((ChecksumTest)tc).testCalculate();
            }
        }));

		ts.addTest(new ChecksumTest("testCheck", new TestMethod() {
            public void run(TestCase tc) {
                ((ChecksumTest)tc).testCheck();
            }
        }));

        return ts;
    }
}

